/**
* This file is part of programmer.
* Description: 
* If you use this code, please cite the respective publications as
* listed on the above website.
*
* programmer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* programmer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with programmer. If not, see <http://www.gnu.org/licenses/>.
*
* @file        longestPalindrome.cpp.c
* @brief       Defines the 
* @author      Ziqiang Wang
* @email       1531651@tongji.edu.cn
* @date        2021/2/17
* @copyright   Copyright (c) 2021
*----------------------------------------------------------------------------*
*  Remark         : Description                                              *
*----------------------------------------------------------------------------*
*  Change History :                                                          *
*  <Date>     | <Version> | <Author>       | <Description>                   *
*----------------------------------------------------------------------------*
*  2021/2/17    | 1.0.0.0   | Ziqiang Wang   | Create file                     *
*----------------------------------------------------------------------------*
*                                                                            *
*/


/*将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 "PAYPALISHIRING" 行数为 3 时，排列如下：

P   A   H   N
A P L S I I G
Y   I   R

之后，你的输出需要从左往右逐行读取，产生出一个新的字符串，比如："PAHNAPLSIIGYIR"。

请你实现这个将字符串进行指定行数变换的函数：

string convert(string s, int numRows);



示例 1：

输入：s = "PAYPALISHIRING", numRows = 3
输出："PAHNAPLSIIGYIR"

示例 2：

输入：s = "PAYPALISHIRING", numRows = 4
输出："PINALSIGYAHRPI"
解释：
P     I    N
A   L S  I G
Y A   H R
P     I

示例 3：

输入：s = "A", numRows = 1
输出："A"



提示：

1 <= s.length <= 1000
s 由英文字母（小写和大写）、',' 和 '.' 组成
1 <= numRows <= 1000

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/zigzag-conversion
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。*/

#include "convert.h"


#include <iostream>
#include <vector>
#include <map>
using namespace std;

constexpr int POSITIVE = 1;
constexpr int NEGTIVE = 0;

class Solution {
public:
    bool print = false;
    string convert(string s, int numRows)
    {
        int row = 0;
        int column = 0;
        int direction = POSITIVE;
        int cycleNum = 0;
        int offset = numRows - 1;
        string res;
        vector<vector<char>> matrix(numRows, vector<char>(s.size(), '0'));

        for (auto &x : s) {
            matrix[row][cycleNum * offset + column] = x;
            if(direction == POSITIVE) {
                row++;
                if(row == numRows) {
                    if (numRows != 2) {
                        direction = NEGTIVE;
                    }
                    row -= 2;
                    row = row < 0 ? 0 : row;
                    column++;
                }
            } else if (numRows != 2){
                row--;
                row = row < 0 ? 0 : row;
                column++;
                if (column == offset) {
                    direction = POSITIVE;
                    column = 0;
                    cycleNum++;
                }
            }
        }

        for(auto &s_x : matrix) {
            for(auto y : s_x) {
                if (print) {
                    std::cout << y << " ";
                }
                if (y != '0') {
                    res.push_back(y);
                }
            }
            if (print) {
                std::cout << "\n";
            }
        }
        return res;
    }
};

int testConvert() {

    Solution s;
    s.convert("PAYPALISHIRING", 3);
}